package cn.upfinder.allvideo;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.upfinder.allvideo.Fragment.VideoListFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = MainActivity.class.getSimpleName();
    @BindView(R.id.rlHome1)
    RadioButton rlHome1;
    @BindView(R.id.rlHome2)
    RadioButton rlHome2;
    @BindView(R.id.rlHome3)
    RadioButton rlHome3;
    @BindView(R.id.rlHome4)
    RadioButton rlHome4;
    @BindView(R.id.flHomeContent)
    FrameLayout flHomeContent;
    @BindView(R.id.rgBottomTab)
    RadioGroup rgBottomTab;

    private FragmentManager fragmentManager;
    //Movie Fragment
    private VideoListFragment videoListFragment;
    private VideoListFragment soukuBeautyDanceFragment;
    private VideoListFragment youKuPlayFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView();

        initListner();

    }

    private void initListner() {

        rgBottomTab.setOnCheckedChangeListener(this);
        rlHome1.setChecked(true);
    }

    private void initView() {
        fragmentManager = getSupportFragmentManager();
        videoListFragment = VideoListFragment.newInstance(VideoListFragment.TYPE_YOUKU_MOVIE);
        fragmentManager.beginTransaction().replace(R.id.flHomeContent, videoListFragment).commit();
    }

    @OnClick({R.id.rlHome1, R.id.rlHome2, R.id.rlHome3, R.id.rlHome4})
    public void onClick(View view) {

    }


    private void selectFragmentShow(int viewId) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (viewId) {
            case R.id.rlHome1:
                if (videoListFragment != null) {
                    transaction.show(videoListFragment);
                } else {
                    videoListFragment = VideoListFragment.newInstance(VideoListFragment.TYPE_YOUKU_MOVIE);
                    transaction.add(R.id.flHomeContent, videoListFragment);
                }
                break;
            case R.id.rlHome2:
                if (soukuBeautyDanceFragment != null) {
                    transaction.show(soukuBeautyDanceFragment);
                } else {
                    soukuBeautyDanceFragment = VideoListFragment.newInstance(VideoListFragment.TYPE_SOUKU_BEAUTY_DANCE);
                    transaction.add(R.id.flHomeContent, soukuBeautyDanceFragment);
                }
                break;
            case R.id.rlHome3:
                if (youKuPlayFragment != null) {
                    transaction.show(youKuPlayFragment);
                } else {
                    youKuPlayFragment = VideoListFragment.newInstance(VideoListFragment.TYPE_YOUKU_PLAY);
                    transaction.add(R.id.flHomeContent, youKuPlayFragment);
                }
                break;
            case R.id.rlHome4:
                break;
        }
        transaction.commit();
    }

    //隐藏所有Fragment
    private void hideFragments(FragmentTransaction fragmentTransaction) {

        if (videoListFragment != null) {
            fragmentTransaction.hide(videoListFragment);
        }

        if (soukuBeautyDanceFragment != null) {
            fragmentTransaction.hide(soukuBeautyDanceFragment);
        }

        if (youKuPlayFragment != null) {
            fragmentTransaction.hide(youKuPlayFragment);
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

        Log.d(TAG, "onClick: " + checkedId);
        selectFragmentShow(checkedId);
    }
}
